package com.c212.manager.controller;

import com.c212.manager.bean.UserInfoEntity;
import com.c212.manager.repository.UserInfoRepository;
import com.c212.manager.utils.UserContext;
import com.c212.manager.utils.annotation.NotLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Random;
import java.util.UUID;

@Controller
@RequestMapping("")
public class IndexController {

    @Autowired
    UserInfoRepository userInfoRepository;

    @GetMapping("/")
    public ModelAndView index() {
        return new ModelAndView("redirect:/article/list");
    }

    @GetMapping("/unauthorized")
    public ModelAndView unauthorized(){
        return new ModelAndView("unauthorized");
    }

    @GetMapping("/admin")
    @ResponseBody
    public Object admin(){

        return UserContext.getUser();
    }

    @GetMapping("/save")
    public Object save() {
        UserInfoEntity userInfoEntity = new UserInfoEntity();
        userInfoEntity.setName("wangwei" + UUID.randomUUID());
//        userInfoEntity.setAge(new Random().nextInt(100));
        userInfoEntity.setCreatedDate(LocalDateTime.now());

        userInfoEntity.setCreatedBy(UserContext.getUser());
        return userInfoRepository.save(userInfoEntity);
    }

    @GetMapping("/search")
    public Object search() {

        return userInfoRepository.findAll();
    }
}
